package com.sec.android.app.myfiles.feature.sidesync;

import com.sec.android.app.myfiles.feature.SidesyncMgr;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;

public class SidesyncImp {
    public static final String SIDESYNC_ACTION_DRAG_DROP_REPLY = "com.sec.android.sidesync.sink.fileUri";
    public static final String SIDESYNC_ACTION_KMS_DRAG_START = "sidesync.app.action.KMS_FILETRANSFER_DRAG_FILEINFO";
    public static final String SIDESYNC_ACTION_PSS_DRAG_START = "com.sec.android.sidesync.source.START_DRAG";

    public static final String SIDESYNC_KMS_FILETRANSFER_PERMISSION = "sidesync.app.action.permission.KMS_FILETRANSFER_DRAG_FILEINFO";

    public static final String SIDESYNC_SOURCE_CONNECTED = "sidesync_source_connect";
    public static final String SIDESYNC_SLINK_CONNECTED = "sidesync_sink_connect";

    public static final int SIDESYNC_DISCONNNECTED = 0;
    public static final int SIDESYNC_CONNNECTED = 1;
    public static final int SIDESYNC_ADDITIONAL_FEATURE = 3;

    public static final String DRAG_DROP_REPLY_VALUE = "fileUri";
    public static final String DRAG_DROP_LABEL = "selectedUri";
    public static final String SELECT_RESULT_PATH = "FILE";

    public SidesyncImp() {

    }

    public boolean isSourceConnected(Context context) {
        boolean bRet = false;

        ContentResolver cr = context.getContentResolver();

        if ((Settings.System.getInt(cr, SIDESYNC_SOURCE_CONNECTED, SIDESYNC_DISCONNNECTED) == SIDESYNC_CONNNECTED)
                || (Settings.System.getInt(cr, SIDESYNC_SOURCE_CONNECTED, SIDESYNC_DISCONNNECTED) == SIDESYNC_ADDITIONAL_FEATURE)) {
            bRet = true;
        }

        Log.d(this, "isSourceConnected - " + bRet);

        return bRet;
    }

    public boolean isSLinkConnected(Context context) {
        boolean bRet = false;

        ContentResolver cr = context.getContentResolver();
        if ((Settings.System.getInt(cr, SIDESYNC_SLINK_CONNECTED, SIDESYNC_DISCONNNECTED) == SIDESYNC_CONNNECTED)) {
            bRet = true;
        }
        
        Log.d(this, "isSLinkConnected - " + bRet);
        return bRet;
    }

    public boolean isKMSRunning(Context context) {
        boolean bRet = false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> infoList = am.getRunningServices(Integer.MAX_VALUE);

        if (infoList != null) {
            for (RunningServiceInfo serviceInfo : infoList) {
                String mServiceName = serviceInfo.service.getClassName();
                if (SidesyncMgr.SIDESYNC_KMS_SINK_SERVICE_CLASS.equals(mServiceName)
                        || SidesyncMgr.SIDESYNC_KMS_SOURCE_SERVICE_CLASS.equals(mServiceName)) {
                    bRet = true;
                    break;
                }
            }
        }

        return bRet;
    }

    public void onDragSidesyncToList(Context context, FileRecord record) {
        Intent intent = new Intent(SIDESYNC_ACTION_DRAG_DROP_REPLY);
        intent.putExtra(DRAG_DROP_REPLY_VALUE, record.getFullPath());
        context.sendBroadcast(intent);
    }

    public void onDragListToKms(Context context, ArrayList<FileRecord> recordList) {
        Intent intent = new Intent(SIDESYNC_ACTION_KMS_DRAG_START);
        intent.putExtra("from-myfiles", true);
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, toUriArrayList(context, recordList));

        context.sendBroadcast(intent, SIDESYNC_KMS_FILETRANSFER_PERMISSION);
    }

    public void onDragToPss(Context context, ArrayList<FileRecord> recordList) {
        Intent intent = new Intent(SIDESYNC_ACTION_PSS_DRAG_START);
        intent.setClipData(toClipData(context, recordList));

        context.sendBroadcast(intent);
    }

    private ClipData toClipData(Context context, ArrayList<FileRecord> recordList) {
        ClipData ret = null;

        for (FileRecord record : recordList) {
            if (ret == null) {
                ret = ClipData.newRawUri(DRAG_DROP_LABEL, record.getUri(context));
            } else {
                ret.addItem(new ClipData.Item(record.getUri(context)));
            }
        }

        return ret;
    }

    private ArrayList<Uri> toUriArrayList(Context context, ArrayList<FileRecord> recordList) {
        ArrayList<Uri> ret = new ArrayList<>();

        for (FileRecord record : recordList) {
            ret.add(record.getUri(context));
        }

        return ret;
    }
}
